/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef NODE_NET_HTTP_HTTP_RESPONSE_H_
#define NODE_NET_HTTP_HTTP_RESPONSE_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <cassert>
#include <event2/http.h>
#include "node/macros.h"
#include "node/net/http/http_request.h"

namespace node {
    namespace http {

        class HttpResponse
        {
        public:
            HttpResponse(::node::http::HttpRequest *request);
            ~HttpResponse(void);

            void Initialize(void);

            bool AddHeader(const char *name, const char *value) {
                return evhttp_add_header(
                    evhttp_request_get_output_headers(request_->request_),
                    name, value) == 0;
            }

            bool Write(const char *data, int size);
            bool WritePrintf(const char *format, ...);
            void End(int code, const char *reason, const char *data = NULL);
            void EndError(int code, const char *reason);

            inline bool IsInitOk(void) const {
                return init_ok_;
            }

        private:
            ::node::http::HttpRequest *request_;
            struct evbuffer *output_;
            bool init_ok_;
            bool end_called_;

            DISALLOW_COPY_AND_ASSIGN(HttpResponse);
        };

    }
}

#endif // NODE_NET_HTTP_HTTP_RESPONSE_H_
